from selenium import webdriver
# from selenium.webdriver.chrome.service import Service
from selenium.webdriver.common.by import By
from time import sleep
from postion import options
from postion import service


class Login():
    def __init__(self, driver):
        self.driver = driver

    def login(self,username,password):
        self.driver.find_element(By.ID, 'ls_username').clear()
        self.driver.find_element(By.ID, 'ls_username').send_keys(username)
        self.driver.find_element(By.ID, 'ls_password').clear()
        self.driver.find_element(By.ID, 'ls_password').send_keys(password)
        sleep(3)
        self.driver.find_element(By.XPATH, '//*[@id="lsform"]/div/div/table/tbody/tr[2]/td[3]/button/em').click()
        sleep(3)
    def register(self,username,password,email):
        self.driver.find_element(By.LINK_TEXT, '立即注册').click()
        self.driver.find_element(By.ID, 'IouQbW').clear()
        self.driver.find_element(By.ID, 'IouQbW').send_keys(username)
        sleep(2)
        self.driver.find_element(By.ID, 'cWFNZj').clear()
        self.driver.find_element(By.ID, 'cWFNZj').send_keys(password)
        sleep(2)
        self.driver.find_element(By.ID, 'd8wZnh').clear()
        self.driver.find_element(By.ID, 'd8wZnh').send_keys(password)
        sleep(2)
        self.driver.find_element(By.ID, 'tu7mNL').clear()
        self.driver.find_element(By.ID, 'tu7mNL').send_keys(email)
        sleep(2)
        self.driver.find_element(By.ID, 'registerformsubmit').click()
        sleep(6)

    def post(self,title,body):
        # 发贴
        self.driver.find_element(By.XPATH, '//*[@id="category_1"]/table/tbody/tr[1]/td[2]/h2/a').click()
        self.driver.find_element(By.ID, 'newspecial').click()
        self.driver.find_element(By.ID, 'subject').clear()
        self.driver.find_element(By.ID, 'subject').send_keys(title)
        sleep(3)
        # 嵌套页面
        self.driver.switch_to.frame('e_iframe')
        self.driver.find_element(By.XPATH, '/html/body').clear()
        self.driver.find_element(By.XPATH, '/html/body').send_keys(body)
        sleep(3)
        # 跳回默认页面
        self.driver.switch_to.default_content()
        self.driver.find_element(By.ID, 'postsubmit').click()
        sleep(3)

    # 回帖
    def reply(self,text):
        self.driver.find_element(By.XPATH, '//*[@id="category_1"]/table/tbody/tr[1]/td[2]/h2/a').click()
        sleep(2)
        self.driver.find_element(By.CLASS_NAME, 's.xst').click()
        sleep(2)
        self.driver.find_element(By.ID, 'post_reply').click()
        sleep(1)
        self.driver.find_element(By.ID, 'postmessage').clear()
        self.driver.find_element(By.ID, 'postmessage').send_keys(text)
        sleep(1)
        self.driver.find_element(By.ID, 'postsubmit').click()
        sleep(3)

    # 看帖
    def show(self):
        self.driver.find_element(By.XPATH, '//*[@id="category_1"]/table/tbody/tr[1]/td[2]/h2/a').click()
        sleep(2)
        self.driver.find_element(By.CLASS_NAME, 's.xst').click()
        sleep(2)

    def logout(self):
        self.driver.find_element(By.LINK_TEXT, '退出').click()
        sleep(3)


if __name__ == '__main__':
    url = r'http://192.168.92.131/upload/'
    username='admin'
    password='123456'
    # username = 'skye'
    # password = '123456'
    # email='alihka@qq.com'
    # title='圆总的风流往事'
    # body='十年后,某圆荣耀归来,报恩也报仇。报唐楚楚救命之恩。报江家灭族之仇。某圆出现在唐楚楚身前:从此后,有我在,你就有全世界'
    # text='圆哥nb'
    driver = webdriver.Chrome(options=options,service=service)
    driver.get(url)

    login = Login(driver)
    login.login(username,password)
    # assert.register(username,password,email)
    # assert.post(title,body)
    # assert.reply(text)
    login.show()
    login.logout()

    driver.quit()
